<!-- 入场申请 -->
<script>
import HPCommonPage from "@/components/HPCommonPage";
import HPTable from "@/components/HPTable";
import HPDialog from '@/components/HPDialog';
// import { receiveAnOrder } from "@/fetch/modules/quoted";
import { modifyAdmit } from "@/fetch/modules/ectrance";
import { admissionTableColumn } from "../js/column";
import { admissionRequireStatus } from "../js/status";
import { approverOption } from "@/views/outsourcedPM/js/status";
import { debounce } from 'lodash';
import getQuery from '@/utils/mixins/getQuery';
import approveFlowPath from '@/utils/mixins/approveFlowPath';
import { remind } from '@/fetch/api'

function fetchEctranceLists(store, opts) {
  return store.dispatch("GET_ECTRANCE_LISTS", opts);
}
export default {
  mixins: [getQuery, approveFlowPath],
  created() {
    const { stus } = this.$route.query;
    if (stus) {
      this.selectData.status = stus;
    }
    this.fetchTableData();
    if (stus) {
      this.$router.push({ query: {} });
    }
  },
  methods: {
    // 提醒
    onRemind(row) {
      remind({
        originId: row.orderId,
        remindCode: 'remind_03'
      }).then(res => {
        const { state, message } = res.data
        if(state === '0000') {
          this.$message({
            message: message,
            type: 'success',
            duration: 1500,
          });
          this.fetchTableData()
        }
      })
    },
    // * 获取列表数据
    async fetchTableData() {
      this.isLoad = true;
      const params = {
        ...this.selectData,
        currentPage: this.selectData.pageCurrent,
        pageNum: this.selectData.pageCurrent,
      }
      await fetchEctranceLists(this.$store, params);
      this.isLoad = false;
      const { state, data } = this.$store.getters.getEctranceLists;
      if (state === "0000") {
        const { list = [], total, lastPage, pageNum } = data || {};
        this.tableData = list || [];
        this.totals = total;
        if(pageNum > lastPage && lastPage > 0) {
          this.selectData.pageCurrent = lastPage
          this.fetchTableData()
        }
      }
    },
    // * 查询
    onSearch() {
      this.selectData.pageCurrent = 1;
      this.searchData = { ...this.selectData }
      this.fetchTableData();
    },
    // * 入场申请
    onToEntrance({ id, userName, needsCvId, approvalState, needId, workCode }) {
      this.$router.push({
        path: "/entrance/apply",
        query: {
          offerId: id,
          userName,
          needsCvId,
          approvalState,
          needId,
          workCode,
          formQuery: JSON.stringify(this.searchData),
        },
      });
    },
    // * 确认入场
     onEnterEntrance:  debounce( async function({ id }) {
      this.fetchEnteringAndCease({
        status: 2,
        offId: id,
      }).then((data) => {
        const { state } = data;
        if (state === "0000") {
          this.fetchTableData();
        }
      });
    }, 1000),
    // * 终止入场
    onQuitEntrance: debounce( async function({ id }) {
      this.$confirm("“终止入场”后，入场流程结束，终止操作无法回转！")
        .then(async (_) => {
          this.fetchEnteringAndCease({
            status: 3,
            offId: id,
          }).then((data) => {
            console.log(data);
            const { state } = data;
            if (state === "0000") {
              this.fetchTableData();
            }
          });
          done();
        })
        .catch((_) => {
          console.log(_);
        });
    }, 1000),
    // * 公共入场接口
    async fetchEnteringAndCease(query) {
      const { data } = await modifyAdmit(query);
      return new Promise((resolve, reject) => {
        if (data.state === "0000") {
          this.$message.success(data.data);
          resolve(data);
        }
      });
    },
    // * 查看
    onLookDetail({ id, status, needsCvId, userName, workCode }) {
      this.$router.push({
        path: `/entrance/detail/${id}`,
        query: {
          stus: status,
          nid: needsCvId,
          sn: userName,
          wc: workCode,
          formQuery: JSON.stringify(this.searchData),
        },
      });
    },
    // * 切换页条数
    handleSizeChange(val) {
      this.selectData.pageSize = val;
      this.searchData = { ...this.selectData }
      this.onSearch();
    },
    // * 切换页数
    handleCurrentChange(val) {
      this.selectData.pageCurrent = val;
      this.searchData = { ...this.selectData }
      this.fetchTableData();
    },
  },
  data() {
    return {
      selectData: {
        // * 条件查询对象
        needsId: "",
        deptName: "",
        postName: "",
        userName: "",
        status: "",
        pageCurrent: 1,
        pageSize: 10,
      },
      // activeName: "toBeArr",
      statusOptions: admissionRequireStatus,
      tableHeaderItems: admissionTableColumn,
      approverStatus: approverOption,
      tableData: [],
      isLoad: false,
      totals: 0,
    };
  },
  components: {
    HPCommonPage,
    HPTable,
    HPDialog
  },
};
</script>

<template>
  <HPCommonPage :titleText="'外包人员管理 / 入场申请'">
    <template v-slot:searchForm>
      <el-form :inline="true" :model="selectData" class="form-box res-form-box">
        <el-form-item label="需求单号：">
          <el-input
            v-model="selectData.needsId"
            clearable
            placeholder="请输入需求单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="岗位名称：">
          <el-input
            v-model="selectData.postName"
            clearable
            placeholder="请输入岗位名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门：">
          <el-input
            v-model="selectData.deptName"
            clearable
            placeholder="请输入部门名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名：">
          <el-input
            v-model="selectData.userName"
            clearable
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="入场状态：">
          <el-select
            v-model="selectData.status"
            placeholder="请选择入场状态"
            clearable=""
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.key"
              :label="item.label"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审批状态：">
          <el-select
            v-model="selectData.approvalState"
            placeholder="请选择入场审批状态"
            clearable=""
            style="min-width: 176px"
          >
            <el-option
              v-for="item in approverStatus"
              :key="item.key"
              :label="item.label"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </template>
    <template v-slot:searchFormbtn>
      <el-button type="success" size="small" @click="onSearch">查询</el-button>
    </template>
    <template v-slot:table>
      <HPTable
        :tableHeaderItems="tableHeaderItems"
        :tableData="tableData"
        :loading="isLoad"
      >
      </HPTable>
    </template>
    <template v-slot:pagination>
      <el-pagination
        v-if="totals"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="selectData.pageCurrent"
        :page-sizes="[10, 20, 30, 40, 50]"
        :page-size="selectData.pageSize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="totals"
      >
      </el-pagination>
    </template>
    <HPDialog
      title="入场申请-审批流程进度"
      width="1200px"
      :visible.sync="approveFlowDialog"
      :before-close="onCloseApproveFlow"
      @close-dialog="onCloseApproveFlow"
      :center="true"
      :footer="null"
      :class="'approve-flow-dialog'"
    >
      <template v-slot:body>
        <approveFlowPath
          :list="approveFlowDate">
        </approveFlowPath>
      </template>
    </HPDialog>
  </HPCommonPage>
</template>

<style lang="scss"></style>
